package com.group2.operations.service.impl;

import com.group2.operations.common.api.ApiResult;
import com.group2.operations.mapper.UserMapper;
import com.group2.operations.model.User;
import com.group2.operations.model.vo.UserInfoVO;
import com.group2.operations.service.UserService;
import com.group2.operations.utils.MD5Utils;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private HistoryService historyService;

    /**
     * 根据用户名查找用户
     * @param name 用户名
     * @return 查找到的用户
     */
    @Override
    public User queryUserByName(String name) {
        return userMapper.queryUserByName(name);
    }

    /**
     * 插入一条用户数据
     * @param user 用户
     * @return 提示信息
     */
    @Override
    public ApiResult insertUser(User user){
        //查找是否有重复用户名
        if(userMapper.queryUserByName(user.getName()) != null) {
            return ApiResult.failed("用户名已存在");
        }

        //插入数据库
        try {
            user.setPwd(MD5Utils.md5(user.getPwd(), MD5Utils.SALT));
            userMapper.insertUser(user);
        } catch (DataAccessException e) {
            e.printStackTrace();
           return ApiResult.failed("数据库内部错误");
        }

        //插入成功
        return ApiResult.success("注册成功");
    }

    /**
     * 更新用户个人信息
     * @param vo 个人信息对象
     * @param username 用户名
     * @return 提示信息
     */
    @Override
    public ApiResult updateUserInfo(UserInfoVO vo, String username) {
        //更新数据库
        try {
            userMapper.updateUserInfo(vo, username);
        } catch (DataAccessException e) {
            e.printStackTrace();
            return ApiResult.failed("数据库内部错误");
        }
        return ApiResult.success("个人信息更新成功");
    }

    /**
     * 获取用户个人信息
     * @param name 用户名
     * @return 用户信息对象
     */
    @Override
    public UserInfoVO getUserInfo(String name) {
        return userMapper.getUserInfoByName(name);
    }

}
